<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;
use App\Company;
use Illuminate\Support\Facades\Hash;

class CompanyController extends Controller
{

    public function register(Request $request) {
        $addCompany['name'] = $request->input('company');
        $count=Company::where('name',$addCompany['name'])->count();
        if($count > 0){
            return response()->json('公司名已存在，添加失败');
            dd();
        }else{
            $company=Company::create($addCompany);
        }

        $addUser['email'] = $request->input('admin');
        $addUser['name'] = $request->input('admin');

        $addUser['password'] = Hash::make($request->input('password'));
        $addUser['company']=$company->id;
        User::create($addUser);
        return response()->json('添加成功');
    }


    public function checkName($company) {

        $count=Company::where('name',$company)->count();

        if($count > 0){
            $r=true;
        }else{
            $r=false;
        }
        return response()->json($r);
    }

    public function getAll() {
        $company=Company::all();
        return response()->json($company);
    }
    public function getCurrent($page) {
        $pages=$page*10-10;
        $company=Company::skip($pages)->take(10)->get();
        return response()->json($company);
    }
    public function one($id) {
        $company=Company::find($id);
        return response()->json($company);
    }
    public function myCompany($id) {
            $company=Company::find($id);
            return response()->json($company);
    }
    public function getCount() {
        $company=Company::all()->count();
        return response()->json($company);
    }
    public function add(Requests\FormRequest $request) {

        $adduser['name'] = $request->input('name');
        $adduser['email'] = $request->input('email');
        $adduser['password'] = Hash::make($request->input('password'));
        $company=Company::create($adduser);
        $addr['uid']=$company->id;
        if ($request->input('rule')){
            $rule=$request->input('rule');
            foreach ($rule as $r){
                $addr['name']=$r;
                Rule::create($addr);
            }
        }
        return response()->json('添加成功');
    }
    public function del ($id) {
        $company=Company::find($id);
        if($company){
            $company->delete();
            return response()->json('删除成功');
        }
        return response()->json('用户不存在');

    }

    // public function update($id, Request $request) {
    //     $adduser = Company::find($id);
    //     if($request->input('password') !='' ){
    //         if($request->input('password') === $request->input('password_confirmation') ){
    //             $adduser->password = Hash::make($request->input('password'));
    //         }else{
    //             return response()->json('密码不相同');
    //             dd();
    //         }
    //     }
    //     $adduser->name = $request->input('name');

    //     $adduser->save();
    //     Rule::where('uid',$id)->delete();
    //     $addr['uid']=$id;
    //     if ($request->input('rule')){
    //         $rule=$request->input('rule');
    //         foreach ($rule as $r){
    //             $addr['name']=$r;
    //             Rule::create($addr);
    //         }
    //     }
    //     return response()->json('修改成功');
    // }
    public function update($id, Request $request) {
        $date=$request->input('date');
        Company::where('id',$id)->update($date);
        
        return response()->json('修改成功');
    }

    public function search($word) {
        $company=Company::where('name','like','%'.$word.'%')->OrWhere('email','like','%'.$word.'%')->get();
        return response()->json($company);
    }
    public function page($page,$pagesize) {
        $skip=$page*$pagesize;
        $take=$pagesize;
        $company=Company::orderBy('id','desc')->skip($skip)->take($take)->get();
        return response()->json($company);
    }
    public function dateOne($date) {

        $company=Company::where('created_at',$date)->get();
        return response()->json($company);
    }
    public function datemore($date) {

        $company=Company::where('created_at',$date)->get();
        return response()->json($company);
    }





}
